package resp

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
)

func Success(c *gin.Context, data interface{}) {
	needPack, valid := c.Get("ResponsePack")
	if valid && needPack.(bool) {
		var responseData = make(map[string]interface{})
		responseData["code"] = 0
		responseData["status"] = 200
		responseData["message"] = ""
		responseData["data"] = data

		c.JSON(200, responseData)
	} else {
		c.JSON(200, data)
	}
}

func Error(c *gin.Context, errType string, msg string) {
	needPack, valid := c.Get("ResponsePack")

	data := GetInfo(errType, msg)

	var responseData = make(map[string]interface{})
	responseData["code"] = data.Code
	responseData["status"] = data.Status
	responseData["message"] = data.Message
	responseData["data"] = data.Data

	if valid && needPack.(bool) {
		c.JSON(200, responseData)
		panic(nil)
	} else {
		c.JSON(data.Status, responseData)
		panic(nil)
	}
}

func SetPages(c *gin.Context, page interface{}) {
	pageStr, _ := json.Marshal(page)
	c.Header("Page", string(pageStr))
}
